package com.dhh.activity;

import java.util.ArrayList;
import java.util.List;
import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem.Type;
import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.CUtil;
import com.dhh.util.Constant;
import com.dhh.util.manager.FoodLogManager;
import com.dhh.util.manager.PlanManager;
import com.dhh.vo.DailyEnergy;
import com.dhh.vo.FoodPlan;
import com.dhh.widget.AmazingAdapter;
import com.dhh.widget.AmazingListView;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ShowFoodLogActivity extends GDActivity implements View.OnClickListener{
	private DataBaseHelper db;
	private AmazingListView lsComposer;
	private SectionComposerAdapter adapter;
	private FoodLogManager flManager;
	private PlanManager pManager;
	private FoodPlan plan;
	
	@Override  
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setActionBarContentView(R.layout.show_food_log); 		

        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();         
        flManager = new FoodLogManager(db);
        pManager = new PlanManager(db);
        
		initActionBar();
		initView();
	}
	
	@Override
	public void onStart() {
        super.onStart();     
        initData();
	}
	
	@Override
    public void onResume(){
    	super.onResume();
        db.openDataBase();       
    }
    
    @Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
    
    @Override
	protected void onStop(){
		super.onStop();
	}
	
	@Override
	public void onClick(View view) {		
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initData(){
		plan= pManager.getCurrentPlanSimple();
		adapter = new SectionComposerAdapter();
		List<Pair<String, List<DailyEnergy>>> logs = new ArrayList<Pair<String, List<DailyEnergy>>>();
		Pair pair = new Pair("",flManager.listAllDailyEnergy());
		logs.add(pair);
		adapter.setData(logs);
		
		if(adapter.getCount()==0){
			lsComposer.setVisibility(View.GONE);
		}else{
			lsComposer.setVisibility(View.VISIBLE);
			lsComposer.setAdapter(adapter);
			lsComposer.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					Intent intent = new Intent(ShowFoodLogActivity.this,FoodLogDetailActivity.class);
					TextView date = (TextView) view.findViewById(R.id.fl_identify);
					intent.putExtra(Constant.C_LOG_DATE, date.getText().toString());
					startActivity(intent);
				}
			});
		}
	}
	
	private void initActionBar(){
		getActionBar().setTitle("My Food Log");
        getActionBar().setBackgroundColor(Color.rgb(14, 166, 249));
        getActionBar().getChildAt(0).
        	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(ShowFoodLogActivity.this,HomeActivity.class);	
					startActivity(intent);
				}}); 
        addActionBarItem(Type.Add).getItemView()
                .setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ShowFoodLogActivity.this,FoodAddActivity.class);	
						intent.putExtra("planId", plan.getId());
						intent.putExtra(Constant.FROM_ACTIVITY, Constant.FA_FOOD_LOG);
						startActivity(intent);
					}}); 
	}
	
	private void initView(){
		lsComposer = (AmazingListView) findViewById(R.id.flComposer);    
        lsComposer.setTextFilterEnabled(true);
		lsComposer.setDivider( null );
        lsComposer.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				TextView foodId = (TextView) view.findViewById(R.id.fs_food_id);
				Intent intent = new Intent(ShowFoodLogActivity.this,FoodAddDetailActivity.class);	
				intent.putExtra("foodId", foodId.getText().toString());
				startActivity(intent);
			}
		});
        
        
	}
	
	class SectionComposerAdapter extends AmazingAdapter {
		List<Pair<String, List<DailyEnergy>>> all = null;
		
		public void setData(List<Pair<String, List<DailyEnergy>>> data){
			all = data;
		}

		@Override
		public int getCount() {
			int res = 0;
			for (int i = 0; i < all.size(); i++) {
				res += all.get(i).second.size();
			}
			return res;
		}

		@Override
		public DailyEnergy getItem(int position) {
			int c = 0;
			for(int i = 0; i < all.size(); i++) {
				if(position >= c && position < c + all.get(i).second.size()) {
					return all.get(i).second.get(position - c);
				}
				c += all.get(i).second.size();
			}
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		protected void onNextPageRequested(int page) {
		}

		@Override
		protected void bindSectionHeader(View view, int position, boolean displaySectionHeader) {
			view.findViewById(R.id.header).setVisibility(View.GONE);
		}

		@Override
		public View getAmazingView(int position, View convertView, ViewGroup parent) {
			View res = convertView;
			if (res == null) res = getLayoutInflater().inflate(R.layout.food_log_item, null);
			TextView date = (TextView) res.findViewById(R.id.fl_date);
			Button state = (Button) res.findViewById(R.id.fl_state);
			TextView energy = (TextView) res.findViewById(R.id.fl_energy);
			TextView progress = (TextView) res.findViewById(R.id.fl_progress);
			TextView ld = (TextView) res.findViewById(R.id.fl_identify);
			
			DailyEnergy item = getItem(position);
			date.setText(CUtil.getDate("yyyy-MM-dd", "EEE, MMM dd", item.getDate()));
			ld.setText(item.getDate());
			if(item.checkHeath()){
				state.setBackgroundResource(R.drawable.ball_green);
			}else{
				state.setBackgroundResource(R.drawable.ball_red);
			}
			energy.setText(item.getEnergy());
			int pg = item.getProgress();
			if(pg>0){
				progress.setText("+"+pg);
				progress.setTextColor(Color.BLUE);
			}else{
				progress.setText(""+pg);
				progress.setTextColor(Color.RED);
			}
			
			return res;
		}

		@Override
		public void configurePinnedHeader(View header, int position, int alpha) {
			TextView lSectionHeader = (TextView)header;
			lSectionHeader.setText(getSections()[getSectionForPosition(position)]);
			lSectionHeader.setBackgroundColor(alpha << 24 | (0xbbffbb));
			lSectionHeader.setTextColor(alpha << 24 | (0x000000));
		}
		

		@Override
		public int getPositionForSection(int section) {
			if (section < 0) section = 0;
			if (section >= all.size()) section = all.size() - 1;
			int c = 0;
			for (int i = 0; i < all.size(); i++) {
				if (section == i) { 
					return c;
				}
				c += all.get(i).second.size();
			}
			return 0;
		}

		@Override
		public int getSectionForPosition(int position) {
			int c = 0;
			for (int i = 0; i < all.size(); i++) {
				if (position >= c && position < c + all.get(i).second.size()) {
					return i;
				}
				c += all.get(i).second.size();
			}
			return -1;
		}

		@Override
		public String[] getSections() {
			String[] res = new String[all.size()];
			for (int i = 0; i < all.size(); i++) {
				res[i] = all.get(i).first;
			}
			return res;
		}
		
	}

}
